<?php

namespace App\Controller\ClickMonitor;

use App\Constants\ChannelTypeEnum;
use App\Controller\AbstractController;
use App\Model\Business\Application;
use App\Model\Report\VivoDevice;
use App\Services\Impl\CommonService;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class VivoController extends AbstractController
{
    #[Inject]
    protected CommonService $commonService;

    public function click(): ResponseInterface
    {
        $requestData = $this->request->all();

        throw_error_if(!$requestData, "没有请求参数");

        var_dump($requestData);

        $appId   = $requestData["appId"] ?? null;
        $channel = $requestData["channel"] ?? null;

        throw_error_if(!$appId || !$channel, "未传应用信息");

//        if (!in_array($channel, ConstantsSupport::channelTypeToChannelList[ChannelTypeEnum::vivo])) {
//            return $this->writeJson(201, [], '渠道号错误');
//        }

        $postData = $requestData[0] ?? null;
        throw_error_if(!$postData, "数据错误");

        $app       = Application::findOrFail($appId);
        $projectId = $app->project_id;

        $oaid            = $postData['oaid'] ?? "";
        $imei            = $postData['imei'] ?? "";
        $requestId       = $postData['requestId'] ?? "";
        $clickTime       = (int)($postData['clickTime'] ?? 0);
        $ua              = $postData['ua'] ?? "";
        $ip              = $postData['ip'] ?? "";
        $creativeId      = (int)($postData['creativeId'] ?? 0);
        $mediaType       = (int)($postData['mediaType'] ?? 0);
        $advertiserId    = $postData['advertiserId'] ?? "";
        $advertiserName  = $postData['advertiserName'] ?? "";
        $advertisementId = (int)($postData['advertisementId'] ?? 0);
        $placeType       = (int)($postData['placeType'] ?? 0);
        $adName          = $postData['adName'] ?? "";
        $groupId         = (int)($postData['groupId'] ?? 0);
        $groupName       = $postData['groupName'] ?? "";
        $campaignId      = (int)($postData['campaignId'] ?? 0);
        $campaignName    = $postData['campaignName'] ?? "";

        if ($oaid == "00000000-0000-0000-0000-000000000000") {
            $oaid = "";
        }

        // 如果oaid没有并且ua或ip没有
        throw_error_if(!$oaid && (!$ua || !$ip), "请求错误");

        VivoDevice::create([
            'project_id'       => $projectId,
            'app_id'           => $appId,
            'channel'          => $channel,
            'oaid'             => $oaid,
            'imei'             => $imei,
            'request_id'       => $requestId,
            'click_time'       => $clickTime,
            'ua'               => $ua,
            'ip'               => $ip,
            'creative_id'      => $creativeId,
            'media_type'       => $mediaType,
            'advertiser_id'    => $advertiserId,
            'advertiser_name'  => $advertiserName,
            'advertisement_id' => $advertisementId,
            'place_type'       => $placeType,
            'ad_name'          => $adName,
            'group_id'         => $groupId,
            'group_name'       => $groupName,
            'campaign_id'      => $campaignId,
            'campaign_name'    => $campaignName,
        ]);

        $this->commonService->insertBigDevice($projectId, $appId, $oaid, $ua, $ip,
            ChannelTypeEnum::VIVO->value, $channel, $advertisementId, $adName, $campaignId, $campaignName);

        return $this->succeed(code: 0);
    }
}